Back to Editorials CodeCraft β
  • Github
  • Instagram
< >

Product of Digits

TIME LIMIT = 1 SEC.

  • The natural numbers begin from 1 and go on up to ∞.
    Given N indices, find the product of the digits that lie on those indices.
    NOTE:
    • Consider that the natural numbers are zero-indexed i.e. the index of the first number in the set of natural numbers is 0, the index of the second number is 1 and so on.
    • Consider that all the natural numbers are written like a string '1234567891011121314….'

    Now, let D be a function that takes an index as it's input and returns the digit that lies on that index.
    D(0)=1;D(1)=2;D(2)=3 and so on…
Input Output
First line will contain N, number of indices.
Second line will contain the Ni indices, each separated by a space.
Print the product of the digits that lie on those indices.
Constraints
  • 1 ≤ N ≤ 10
  • 1 ≤ Ni ≤ 1000
Example Test Case
Input             Output
5
1 2 3 4 5
720
Solution

#include <iostream> #include<set> #include<string> using namespace std; int main() { int n,i,temp; cin>>n; long product=1; multiset<int> indices; //stores the indices multiset<int>::reverse_iterator it; //to interate in reverse order string digits; //to store number as string //take indices for(i=0;i<n;i++) { cin>>temp; indices.insert(temp); } //start from last it = indices.rbegin(); //append each number to string. for(i=1;i<=*it+1;i++) digits += to_string(i); //get the digit at index and multiply with product. for(it=indices.rbegin();it!=indices.rend();++it) product *= (digits[*it]-48); //convert to int and multiply cout<<product; //display product. return 0; }
  • #1 Thieves
  • #2 The Queue of Doubts
  • #3 Summation of Primes
  • #4 Spacious Maximus
  • #5 Samosas
  • #6 Reasonably Sound
  • #7 Product of Digits
  • #8 Prime Usernames
  • #9 Path Shifter
  • #10 Keypad Count
  • #11 Even Vowels
  • #12 Encryption
  • #13 Decryption
  • #14 Canteen Accountant
  • #15 Attendance Register
  • #16 Amazing Year

Get in touch

  • executives@codingstudio.club
  • +91 9010342360
  • Vignana Bharthi Instute of Technology
    Aushapur, Ghatkesar, Hyderabad, Telengana - India

© coding.Studio();